package guestbook;

import java.io.IOException;
import java.util.Date;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
import javax.servlet.http.*;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import guestbook.PK_data;
import guestbook.XMLReader;
import guestbook.PMF;

public class SavePKdataServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger(SavePKdataServlet.class.getName());

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
				throws IOException {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		String name = "Nobody";
		if(user != null) name = user.getNickname();
		PK_data[] result_set = guestbook.XMLReader.parse_PK_data();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		log.info("Persistence Manager opened.");
		try {
						for (int j = 0; j < result_set.length; j++) {
								pm.makePersistent(result_set[j]);
								log.info("  saved object " + j + ".");
						}
		} finally {
			pm.close();
			log.info("Persistence Manager closed.");
		}
		resp.sendRedirect("/guestbook.jsp");
	}
}
